package com.ruoyi.workorder.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.workorder.service.IWorkOrderService;
import org.apache.poi.ss.formula.functions.Count;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workorder.domain.WorkordersCatalog;
import com.ruoyi.workorder.service.IWorkordersCatalogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单类别Controller
 * 
 * @author renyuxin
 * @date 2024-05-28
 */
@RestController
@RequestMapping("/workorder/workorderType")
public class WorkordersCatalogController extends BaseController
{
    @Autowired
    private IWorkordersCatalogService workordersCatalogService;

    @Autowired
    private IWorkOrderService workOrderService;



    /**
     * 查询工单类别列表
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkordersCatalog workordersCatalog)
    {
        startPage();
        List<WorkordersCatalog> list = workordersCatalogService.selectWorkordersCatalogList(workordersCatalog);
        return getDataTable(list);
    }

    /**
     * 导出工单类别列表
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:export')")
    @Log(title = "工单类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkordersCatalog workordersCatalog)
    {
        List<WorkordersCatalog> list = workordersCatalogService.selectWorkordersCatalogList(workordersCatalog);
        ExcelUtil<WorkordersCatalog> util = new ExcelUtil<WorkordersCatalog>(WorkordersCatalog.class);
        util.exportExcel(response, list, "工单类别数据");
    }

    /**
     * 获取工单类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:query')")
    @GetMapping(value = "/{woCatalogId}")
    public AjaxResult getInfo(@PathVariable("woCatalogId") Long woCatalogId)
    {
        return success(workordersCatalogService.selectWorkordersCatalogByWoCatalogId(woCatalogId));
    }

    /**
     * 新增工单类别
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:add')")
    @Log(title = "工单类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkordersCatalog workordersCatalog)
    {

        //唯一性验证
        if(workordersCatalogService.checkNameUnique(workordersCatalog)){
            return error("工单类别名称已存在,请换一个!");
        }
        return toAjax(workordersCatalogService.insertWorkordersCatalog(workordersCatalog));
    }

    /**
     * 修改工单类别
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:edit')")
    @Log(title = "工单类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WorkordersCatalog workordersCatalog)
    {
        //唯一性验证
        if(workordersCatalogService.checkNameUnique(workordersCatalog)){
            return error("工单类别名称已存在,请换一个!");
        }
        return toAjax(workordersCatalogService.updateWorkordersCatalog(workordersCatalog));
    }

    /**
     * 删除工单类别
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:remove')")
    @Log(title = "工单类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{woCatalogIds}")
    public AjaxResult remove(@PathVariable Long[] woCatalogIds)
    {
        // 用于存储不能删除的工单类别ID
        List<Long> unremovableIds = new ArrayList<>();
        //有约束删除
        StringBuilder sb = new StringBuilder();
        for (Long woCatalogId : woCatalogIds) {
            List<String> hasRelated = workOrderService.hasRelated(woCatalogId);
            if(!hasRelated.isEmpty()){
                unremovableIds.add(woCatalogId);
                sb.append(hasRelated.get(0)).append(",");
            }else{
                int i = workordersCatalogService.deleteWorkordersCatalogByWoCatalogId(woCatalogId);
            }
        }
        // 移除最后的逗号和空格
        if (sb.length() > 0) {
            sb.setLength(sb.length() - 2);
        }
        // 检查是否有不能删除的工单类别
        if (!unremovableIds.isEmpty()) {
            return error("以下工单类别被工单表使用,无法删除: " + unremovableIds);
        }
        return toAjax(1);
    }
}
